﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Core.Office.VariantTypes;
using DocumentFormat.OpenXml.Lite.Core.Shared;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Samples.Word
{
    class DocumentProperties
    {
        public static void Create()
        {
            Document doc = DocumentManager.Instance.CreateWordDocument();
            doc.CreateBody().AddParagraph().AddRun().AddSimpleText("Hello world");

            doc.AppProperties = new AppProperties()
            {
                Template = new SimpleElement<string>("Normal"),
                Pages = new SimpleElement<int>(1),
                TitlesOfParts = new VectorContainer()
            };
            doc.AppProperties.TitlesOfParts.Element = new Vector()
            {
                Size = 1,
                BaseType = ST_VectorBaseType.LPSTR
            };
            doc.AppProperties.TitlesOfParts.Element.Elements.Add(new LPSTR());

            doc.CoreProperties = new CoreProperties()
            {
                Creator = new SimpleElement<string>("Test user"),
                LastModifiedBy = new SimpleElement<string>("Test user"),
                Revision = new SimpleElement<string>("1"),
                Created = new XsiItem<DateTime>(DateTime.Now, "dcterms:W3CDTF"),
                Modified = new XsiItem<DateTime>(DateTime.Now, "dcterms:W3CDTF"),
            };

            doc.Save(@"samples\docprops.docx");

            Document parsed = null;
            if (DocumentManager.Instance.OpenWordDocument(@"samples\docprops.docx", out parsed))
            {
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
